package com.itao.rocketmq5.fifo;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.itao.rocketmq5.util.RocketMqFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.producer.SendReceipt;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @Author sjt
 * @CreateTime 2023/10/11 14:16
 */
@Slf4j
public class FifoProducer {
    private final static String FIFO_TOPIC = "fifo_topic";
    private final static String MESSAGE_GROUP = "fifoGroup";

    public static void main(String[] args) {
        var producer = RocketMqFactory.getProducer(FIFO_TOPIC);
        var message = RocketMqFactory.provider().newMessageBuilder()
                .setKeys("fifoTopicKey")
                .setTag("fifoTopic")
                .setTopic(FIFO_TOPIC)
                // 设置顺序消息的排序分组，该分组尽量保持离散，避免热点排序分组。
                .setMessageGroup(MESSAGE_GROUP)
                .setBody("fifoTopicMessage".getBytes(StandardCharsets.UTF_8))
                .build();
        try {
            //发送消息，需要关注发送结果，并捕获失败等异常。
            SendReceipt sendReceipt = producer.send(message);
            log.info("SendReceipt:{}", sendReceipt);
        } catch (ClientException e) {
            log.error("异常", e);
        }
    }
}
